package mapreduce;

import java.nio.ByteBuffer;

public class StringFormat extends BytesFormat{

	public BytesFormat getFormat(){
		return new StringFormat();
	}
	
	@Override
	public Object decode(ByteBuffer buffer) {
		int length = buffer.getInt();
		byte[] outBytes = new byte[length];
		buffer.get(outBytes);
		String object = new String(outBytes);
		outBytes = null;
		return object;
	}

	@Override
	public ByteBuffer encode(Object object, ByteBuffer buffer) {
		String stringObject = (String)object;
		byte[] bytes = stringObject.getBytes();
		ByteBuffer newBuffer = BufferChecker.check(buffer, 4 + bytes.length);
		newBuffer.putInt(bytes.length);
		newBuffer.put(bytes);
		bytes = null;
		return newBuffer;
	}

	@Override
	protected int getCode() {
		// TODO Auto-generated method stub
		return 2;
	}

}
